//
//  WPStatusPictureView.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/8.
//  Copyright © 2017年 汪鹏. All rights reserved.
//

import UIKit

class WPStatusPictureView: UIView {

    // 高度的约束
    @IBOutlet weak var heightCons: NSLayoutConstraint!

    // 单条微博视图模型
    var viewModel:WPSingleWBViewModel? {
        
        didSet{
            
            // 修改配图视图大小
            reSizeView()
            
        }
        
    }
    
    /// 修改配图视图大小
    private func reSizeView() {
       
        
        /// 修改宽度
        let viewSize = viewModel?.pictureViewSize ?? CGSize()
        
        let item = subviews[0]

        // 单张图,修改subViews[0]的大小
        if viewModel?.pic_urls?.count == 1 {
            
            item.frame = CGRect(x: 0,
                                 y: PictureViewTopMargin,
                                 width: viewSize.width,
                                 height: viewSize.height-PictureViewTopMargin)
        }else { // 多图，恢复subViews[0]的大小
            
            item.frame = CGRect(x: 0,
                                y: PictureViewTopMargin,
                                width: PictureViewItemWidth,
                                height: PictureViewItemHeight)
            
        }
        
        /// 修改高度
        heightCons.constant = viewModel?.pictureViewSize.height ?? 0
        
    }
    
    // 图片url数组
    var urls:[WPStatusPictures]? {
        
        didSet{
            
            // 隐藏所有的图片视图
            for picture in subviews{
                
                picture.isHidden = true
                
            }
            
            if urls?.count == 0 || urls == nil {
                return
            }
            
            /* 测试2*2排列
            if urls!.count > 4 {
                
                var d = urls
                
                d?.removeSubrange((d?.startIndex)!+4...(d?.count)!-1)
                
                urls = d
                
            }
            */
            
            // 遍历urls数组设置数据
            var i = 0
            for url in urls! {
                
                let pic = subviews[i] as! UIImageView
                
                pic.isHidden = false
                
                pic.wp_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                if i == 1 && urls?.count == 4 {
                    
                    i += 1
                }
                
                i += 1
                
            }
            
        }
        
    }
    override func awakeFromNib() {
        
        setupUI()
    }
    
    /// 布局子视图
    private func setupUI() {
        
        clipsToBounds = true
        
        // backgroundColor = superview?.backgroundColor
        
        let maxCol = 3
        
        let rect = CGRect(x: 0, y: PictureViewTopMargin, width: PictureViewItemWidth, height: PictureViewItemWidth)
        
        for i in 0..<maxCol * maxCol {
            
            let row = CGFloat(i / maxCol)
            
            let col = CGFloat(i % maxCol)
            
            let offSetX = (PictureViewInnerMargin + PictureViewItemWidth) * col
            
            let offSetY = (PictureViewInnerMargin + PictureViewItemWidth) * row
            
            let imgView = UIImageView()
            
            imgView.contentMode = .scaleAspectFill
            
            imgView.clipsToBounds = true
            
            imgView.frame = rect.offsetBy(dx: offSetX, dy: offSetY)
            
            addSubview(imgView)
            
        }
        
    }
}

